//
//  RPhotoViewController.m
//  Riji
//
//  Created by  on 2022/8/19.
//

#import "RPhotoViewController.h"
#import "RPhotoCell.h"
#import "RPhotoHeader.h"
#import "RNoteCell.h"
#import "RPictureInfoViewController.h"

@interface RPhotoViewController ()
@property (strong, nonatomic) IBOutlet UICollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray *data;
@end

@implementation RPhotoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initUI];
    [self loadData];
}
- (void)initUI{
    self.title = LS(@"日记图库");
    [self.collectionView registerNib:[RPhotoCell nib] forCellWithReuseIdentifier:[RPhotoCell ID]];
    [self.collectionView registerNib:[RPhotoHeader nib] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:[RPhotoHeader ID]];
}
- (void)loadData{
    [RLHud startHud:LS(@"获取数据中...")];
    NSString* where = [NSString stringWithFormat:@"where %@=%@ and %@=%@",bg_sqlKey(@"ID"),bg_sqlValue(APP.user.ID),bg_sqlKey(@"remove"),bg_sqlValue(@(NO))];
    [RNoteModel bg_findAsync:nil where:where complete:^(NSArray * _Nullable array) {
        dispatch_async(dispatch_get_main_queue(), ^{
            self.data = [NSMutableArray arrayWithArray:array];
            [self.collectionView reloadData];
            [RLHud stopHud];
        });
    }];
}
#pragma mark - UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return  self.data.count;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    RNoteModel *model = self.data[section];
    return  model.data.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    RPhotoCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:[RPhotoCell ID] forIndexPath:indexPath];
    RNoteModel *model = self.data[indexPath.section];
    cell.img.image = [UIImage imageWithData:model.data[indexPath.row]];
    return  cell;
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    RPhotoHeader *header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:[RPhotoHeader ID] forIndexPath:indexPath];
    RNoteModel *model = self.data[indexPath.section];
    header.date.text = [NSDate transToDate:@"yyyy-MM-dd HH:mm" :model.stamp];
    header.weak.text = [RNoteCell week:[model.week intValue]];
    return  header;
}
#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    RPictureInfoViewController *vc = [RPictureInfoViewController new];
    vc.index = (int)indexPath.row + 1;
    vc.model = self.data[indexPath.section];
    [self.navigationController pushViewController:vc animated:YES];
}
#pragma mark - UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat w = (SCREEN_WIDTH - 15 * 4) / 3;
    return  CGSizeMake(w,w);
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return  UIEdgeInsetsMake(0, 15, 0, 15);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 15;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 15;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    return CGSizeMake(SCREEN_WIDTH, 70);
}
@end
